package com.ruoyi.project.system.service.impl;

import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.College;
import com.ruoyi.project.system.domain.CollegePaperAnswerDetail;
import com.ruoyi.project.system.domain.Student;
import com.ruoyi.project.system.mapper.CollegeMapper;
import com.ruoyi.project.system.mapper.CollegePaperAnswerDetailMapper;
import com.ruoyi.project.system.mapper.CollegePaperMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.mapper.CollegePaperAnswerMapper;
import com.ruoyi.project.system.domain.CollegePaperAnswer;
import com.ruoyi.project.system.service.ICollegePaperAnswerService;

/**
 * 学院试卷答题Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-11-19
 */
@Service
public class CollegePaperAnswerServiceImpl implements ICollegePaperAnswerService 
{
    @Autowired
    private CollegePaperAnswerMapper collegePaperAnswerMapper;

    @Autowired
    private CollegePaperMapper collegePaperMapper;

    @Autowired
    private CollegePaperAnswerDetailMapper collegePaperAnswerDetailMapper;


    /**
     * 查询学院试卷答题
     * 
     * @param id 学院试卷答题ID
     * @return 学院试卷答题
     */
    @Override
    public CollegePaperAnswer selectCollegePaperAnswerById(Long id)
    {
        return collegePaperAnswerMapper.selectCollegePaperAnswerById(id);
    }

    /**
     * 查询学院试卷答题列表
     * 
     * @param collegePaperAnswer 学院试卷答题
     * @return 学院试卷答题
     */
    @Override
    public List<Map<String,Object>> selectCollegePaperAnswerList(CollegePaperAnswer collegePaperAnswer)
    {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        if(loginUser.getUser().getUserId()!=1){
            collegePaperAnswer.setCollegeId(loginUser.getCollegeStaff().getCollegeId().longValue());
        }
        return collegePaperAnswerMapper.selectCollegePaperAnswerList(collegePaperAnswer);
    }

    /**
     * 新增学院试卷答题
     * 
     * @param collegePaperAnswer 学院试卷答题
     * @return 结果
     */
    @Override
    public int insertCollegePaperAnswer(CollegePaperAnswer collegePaperAnswer)
    {
        Student student = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getStudent();

        //学校标识，名称
        collegePaperAnswer.setCollegeId(student.getCollegeId().longValue());
        collegePaperAnswer.setCollegeName(student.getCollegeName());


        //答题人标识，姓名，证件号
        collegePaperAnswer.setStudentId(student.getId().longValue());
        collegePaperAnswer.setName(student.getRealName());
        collegePaperAnswer.setIdcardCode(student.getIdcardCode());

        //回写试卷表中的参考人数

        collegePaperMapper.updateTesterCount(collegePaperAnswer.getCollegePaperId());

        /**
         * int a = bigdemical.compareTo(bigdemical2)
         *
         * a = -1,表示bigdemical小于bigdemical2；
         *
         * a = 0,表示bigdemical等于bigdemical2；
         *
         * a = 1,表示bigdemical大于bigdemical2；
         */

        //合格人数
        if(collegePaperAnswer.getScore().compareTo(collegePaperAnswer.getStandardScore())==1){
            collegePaperMapper.updatePasserCount(collegePaperAnswer.getCollegePaperId());
        }

        collegePaperAnswerMapper.insertCollegePaperAnswer(collegePaperAnswer);

        for (CollegePaperAnswerDetail detail : collegePaperAnswer.getList()) {

            detail.setContent(URLDecoder.decode(detail.getContent()));
            detail.setCollegePaperAnswerId(collegePaperAnswer.getId());
            collegePaperAnswerDetailMapper.insertCollegePaperAnswerDetail(detail);

        }


        return 1;
    }

    /**
     * 修改学院试卷答题
     * 
     * @param collegePaperAnswer 学院试卷答题
     * @return 结果
     */
    @Override
    public int updateCollegePaperAnswer(CollegePaperAnswer collegePaperAnswer)
    {
        return collegePaperAnswerMapper.updateCollegePaperAnswer(collegePaperAnswer);
    }

    /**
     * 批量删除学院试卷答题
     * 
     * @param ids 需要删除的学院试卷答题ID
     * @return 结果
     */
    @Override
    public int deleteCollegePaperAnswerByIds(Long[] ids)
    {
        return collegePaperAnswerMapper.deleteCollegePaperAnswerByIds(ids);
    }

    /**
     * 删除学院试卷答题信息
     * 
     * @param id 学院试卷答题ID
     * @return 结果
     */
    @Override
    public int deleteCollegePaperAnswerById(Long id)
    {
        return collegePaperAnswerMapper.deleteCollegePaperAnswerById(id);
    }
}
